<template>
	<view class="flex_c_c page">
		<view class="flex_r fa_c fj_b top" :style="{top:$store.state.StatusBar.statusBar+'px'}">
			<view class="top-icon" @click="to()">
				<image class="img" src="" mode="aspectFill"></image>
			</view>
			<view class="top-icon">
			</view>
		</view>
		<view class="text_38 title">
			账号注册
		</view>
		<view class="form">
			<view class="flex_r form-row">
				<view class="text_34 form-row-title">
					+86
				</view>
				<view class="flex1 form-row-input">
					<input v-model="formData.loginname" type="text" placeholder="请输入手机号">
				</view>
				<view class="m-line">
					<m-line color="#B8B8B8" length="100%" :hairline="true"></m-line>
				</view>
			</view>
			<view class="flex_r form-row">
				<view class="text_34 form-row-title">
					验证码
				</view>
				<view class="flex1 form-row-input">
					<input v-model="formData.code" type="text" placeholder="请输入验证码">
				</view>
				<view class="form-row-code" @click="sms">
					获取验证码
				</view>
				<view class="m-line">
					<m-line color="#B8B8B8" length="100%" :hairline="true"></m-line>
				</view>
			</view>

			<view class="flex_r form-row">
				<view class="text_34 form-row-title">
					密码
				</view>
				<view class="flex1 form-row-input">
					<input v-model="formData.password" type="text" placeholder="请输入密码">
				</view>
				<view class="m-line">
					<m-line color="#B8B8B8" length="100%" :hairline="true"></m-line>
				</view>
			</view>
			<view class="flex_r form-row">
				<view class="text_34 form-row-title">
					邀请码
				</view>
				<view class="flex1 form-row-input">
					<input v-model="formData.share" type="text" placeholder="请输入邀请码">
				</view>
				<view class="m-line">
					<m-line color="#B8B8B8" length="100%" :hairline="true"></m-line>
				</view>
			</view>

		</view>

		<view class="botton">
			<view class="size_white text_30 button" @click="submit">
				注册
			</view>
			<view class="icon_ agreement">
				<view class="icon_ checkbox" @click="change">
					<view class="checkbox-box" v-if="checked">
						<image class="img" src="" mode="aspectFill"></image>
					</view>
				</view>
				<view class="">
					同意用户协议<用户协议>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	import md5 from '@/utils/md5.js';
import { genTestUserSig } from '../../../im/debug/GenerateTestUserSig.js';
	import {
		show,
		to
	} from '@/utils/index.js';
	export default {
		data() {
			return {
				checked:false,
				formData: {
					loginname: '',
					code: '',
					password: '',
					share: ''
				}
			}
		},
		onLoad() {
		},
		methods: {
			to,
			change(e) {
				this.checked = !this.checked 
			},
			async sms(){
				let param = {
					loginname: this.formData.loginname,
					type: '1'
				};
				let res = await this.$c.post('/app/zhuce/sendCode', param)
				if(res.code != 0){
					uni.showToast({
					  title: res.msg,
					  duration: 1000,
					  icon: 'none'
					});
				}else{
					uni.showToast({
					  title: '短信已发送',
					  duration: 1000,
					  icon: 'none'
					});
				}
			},
			async loginTo() {
			  let userID = this.username;
			  
			  // 发送请求
			  // {loginname:'13950104779',password:'e10adc3949ba59abbe56e057f20f883e',loginType:1}
			  console.log(md5)
			  let res = await this.$c.post('/app/login/login?loginname='+this.formData.loginname+
			  '&password='+md5.hex_md5(this.formData.password)+'&loginType=1');
			  // let res = await this.$c.post('/app/login/login?loginname=13215061595&password=e10adc3949ba59abbe56e057f20f883e&loginType=1');
			  if (res.code != 0) {
			  	this.$c.show(res.msg);
			  	return;
			  }
			  let token = res.data.token
			  userID = res.data.user.tengxuncode.toString();
			  const userSig = genTestUserSig(userID).userSig;
			  //获得sign
			 //  res = await this.$c.post('/app/tx/getUserSig);
			 //  if (res.code != 0) {
				// this.$c.show(res.msg);
				// return;
			 //  }
			 //  userSig = res.data.userSig;
			  app.globalData.userInfo = {
				token,
			    userSig,
			    userID
			  };
			
			  uni.setStorageSync('userInfo', app.globalData.userInfo);
			
			  // 登录腾讯im
			  uni.$TUIKit
			    .login({
			      userID: userID,
			      userSig: userSig
			    })
			    .then(res => {
			      this.$store.commit('setToken', app.globalData.userInfo);
			      uni.showToast({
			        title: '登录中...',
			        duration: 3000,
			        icon: 'none'
			      });
			      setTimeout(() => {
			        uni.switchTab({
			          // url: '/pages/msg'
					  // url: '/pages/m-pages/m-home/index',
					  url: '/pages/m-pages/m-personnelList/index'
			        });
			      }, 3000);
			    })
			    .catch((err) => {
					console.log(err)
					// setTimeout(() => {
					//   uni.switchTab({
					//     // url: '/pages/msg'
					// 			  // url: '/pages/m-pages/m-home/index',
					// 			  url: '/pages/m-pages/m-personnelList/index'
					//   });
					// }, 3000);
				});
			  // 登录 native 原生通话插件
			  // #ifdef  APP-PLUS
			  uni.$TUICallKit.login(
			    {
			      sdkAppID: genTestUserSig('').sdkAppID,
			      userID: app.globalData.userInfo.userID,
			      userSig: app.globalData.userInfo.userSig
			    },
			    res => {
			      console.log('app视频通话登录成功');
			    }
			  );
			  // #endif
			},
			submit() {
				this.loginTo()
				// let formDataCheck = {
				// 	a: '请输入a',
				// 	b: '请输入b',
				// 	c: '请输入c',
				// 	d: '请输入d'
				// };
				// let data = {}
				// for (const key in this.formData) {
				// 	if (this.formData[key]) {
				// 		data[key] = this.formData[key]
				// 	} else {
				// 		return show(`请填写${formDataCheck[key]}`);
				// 		break;
				// 	}
				// }
				// console.log(data)
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {
		position: relative;
		box-sizing: border-box;
		padding: 0 50rpx;
		width: 100%;
		height: 100vh;
		margin: 0 auto;
	}

	.top {
		position: absolute;
		top: 0;
		left: 40rpx;
		width: 100%;
		height: 100rpx;
		.top-icon {
			width: 44rpx;
			height: 44rpx;
		}
	}

	.title {
		width: 100%;
		height: 200rpx;
		line-height: 200rpx;
		margin-bottom: 20rpx;
	}

	.form {
		width: 100%;

		.form-row {
			position: relative;
			width: 100%;
			height: 100rpx;
			margin-bottom: 20rpx;

			.form-row-title {
				width: 170rpx;
				line-height: 100rpx;
			}

			.form-row-input {
				height: 100%;

				input {
					width: 100%;
					height: 100%;
				}
			}

			.form-row-code {
				box-sizing: border-box;
				padding-left: 10rpx;
				line-height: 100rpx;
				color: #e6166a;
			}

			.m-line {
				position: absolute;
				width: 100%;
				left: 0;
				bottom: 0;
				right: 0;
			}
		}
	}

	.botton {
		width: 100%;
		margin-top: 140rpx;
		margin-bottom: 100rpx;

		.button {
			width: 100%;
			height: 90rpx;
			border-radius: 20rpx;
			text-align: center;
			line-height: 90rpx;
			background-color: #e6166a;
			margin-bottom: 20rpx;
		}

		.agreement {
			width: 100%;
			height: 50rpx;

			.checkbox {
				width: 48rpx;
				height: 48rpx;
				border-radius: 50%;
				box-sizing: border-box;
				border: 1px solid #e6166a;
				margin-right: 20rpx;
				.checkbox-box {
					width: 40rpx;
					height: 40rpx;
					box-sizing: border-box;
				}
			}
		}

	}
</style>